package com.aco.fatureapp.auxiliary;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothClass;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.aco.fatureapp.R;
import com.aco.fatureapp.enums.EnumBluetoothClass;

public class DeviceAdapter extends BaseAdapter {

	private static final int TYPE_ITEM = 0;
	private static final int TYPE_SEPARATOR = 1;

	public static final boolean CLICKABLE = true;
	public static final boolean NOT_CLICKABLE = false;

	public static final boolean SHOW_PROGRESS = true;
	public static final boolean HIDE_PROGRESS = false;

	private List<DeviceItem> mData = new ArrayList<DeviceItem>();
	private TreeSet<Integer> sectionHeader = new TreeSet<Integer>();

	private LayoutInflater mInflater;

	public DeviceAdapter(Context context) {
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public void addItem(final String item, final BluetoothClass bluetoothClass,
			final boolean clickable) {
		mData.add(new DeviceItem(item, clickable, bluetoothClass));
		notifyDataSetChanged();
	}

	public void addSectionHeaderItem(final String item,
			final boolean showProgress) {
		mData.add(new DeviceItem(item, Boolean.FALSE, showProgress));
		sectionHeader.add(mData.size() - 1);
		notifyDataSetChanged();
	}

	@Override
	public int getItemViewType(int position) {
		return sectionHeader.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
	}

	@Override
	public int getViewTypeCount() {
		return 2;
	}

	@Override
	public int getCount() {
		return mData.size();
	}

	@Override
	public DeviceItem getItem(int position) {
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	@SuppressLint("InflateParams")
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		int rowType = getItemViewType(position);

		if (convertView == null) {
			holder = new ViewHolder();
			switch (rowType) {
			case TYPE_ITEM:
				convertView = mInflater.inflate(R.layout.adapter_devices_row,
						null);

				holder.icon = (ImageView) convertView
						.findViewById(R.id.imgBluetoothClass);

				holder.textView = (TextView) convertView
						.findViewById(R.id.txtDeviceName);

				if (holder.progressBar != null) {
					holder.progressBar.setVisibility(View.VISIBLE);
				}

				break;
			case TYPE_SEPARATOR:
				convertView = mInflater.inflate(
						R.layout.adapter_devices_header, null);

				holder.progressBar = (ProgressBar) convertView
						.findViewById(R.id.progressBar);

				holder.textView = (TextView) convertView
						.findViewById(R.id.txtHeader);

				break;
			}

			convertView.setTag(holder);
		}
		else {
			holder = (ViewHolder) convertView.getTag();
		}

		/*
		 * Setando o icone de acordo com a classe do dispositivo
		 */
		BluetoothClass bluetoothClass = mData.get(position).getBluetoothClass();
		if (holder.icon != null && bluetoothClass != null) {
			Drawable icon = convertView.getResources().getDrawable(
					EnumBluetoothClass.getValueOf(bluetoothClass
							.getDeviceClass()));

			holder.icon.setImageDrawable(icon);
		}

		/*
		 * Verificando a necessidade de exibir o load da busca.
		 */
		if (mData.get(position).isShowProgress() && holder.progressBar != null) {
			holder.progressBar.setVisibility(View.VISIBLE);
		}
		else if (holder.progressBar != null) {
			holder.progressBar.setVisibility(View.GONE);
		}

		holder.textView.setText(mData.get(position).getTitle());

		/*
		 * Caso um item da lista não seja clicável.
		 */
		if (!mData.get(position).isClickable()) {
			convertView.setOnClickListener(null);
		}

		return convertView;
	}

	public void clear() {
		mData.clear();
		notifyDataSetChanged();
	}

	public static class ViewHolder {
		ProgressBar progressBar;
		TextView textView;
		ImageView icon;
	}
}